package com.tro.common.util.email;

import java.io.File;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;

import org.apache.commons.lang3.StringUtils;

import com.tro.common.util.string.AESTool;

/**
 * 
 * @ClassName: MailUtil
 * @Description: TODO(发送邮件)
 * @author leeko
 * @date 2017年11月3日 下午2:20:52
 *
 */
public class MailUtil {
	
	public static boolean sendSigln(String email, String password, List<Map<String, Object>> attachments, List<String> recipients,
			String content, String title) {
		boolean falg = false;
		if (StringUtils.isBlank(email)) {
			throw new RuntimeException("邮箱地址为空");
		}
		// 邮件发送
		try {
			String subject = "";// 邮件主题
			
			// 第二步，添加发送协议
			Properties props = null;
			props = System.getProperties();
			// 协议判定
			String[] str = email.split("@");
			if (str.length != 2) {
				throw new RuntimeException("邮箱地址有误");
			}
			//根据发送邮箱的账号解析出smtp服务器  
			String smtpHostName = "smtp." + email.split("@")[1];  
            //初始化props  
			props.put("mail.smtp.ssl.enable", "true");  
			props.put("mail.smtp.host", smtpHostName);

			Session session = null;
			MimeMessage mimeMsg = null;
			Multipart mp = null;
			try {
				session = Session.getDefaultInstance(props, null);// 发送email首先要获取session对象
				mimeMsg = new MimeMessage(session);
				mp = new MimeMultipart();
			} catch (Exception e) {
				throw new RuntimeException("获取Mail Session对象失败", e);
			}
			props.put("mail.smtp.auth", "true");
			// 第三步，添加邮件主题
			mimeMsg.setSubject(MimeUtility.encodeText(subject, MimeUtility.mimeCharset("utf-8"), title));
			// 第四步，添加邮件正文
			BodyPart bp = new MimeBodyPart();
			bp.setContent("<meta http-equiv=Content-Type content=text/html; charset=UTF-8>" + content,
					"text/html;charset=UTF-8");
			mp.addBodyPart(bp);
			mimeMsg.setFrom(new InternetAddress(email));

			// 第五步，添加收件人，邮件群发
			InternetAddress[] addresses = new InternetAddress[recipients.size()];
			for (int i = 0; i < addresses.length; i++) {
				addresses[i] = new InternetAddress(recipients.get(i));
			}
			mimeMsg.setRecipients(Message.RecipientType.TO, addresses);// 收件人
			// 附件群发
			if (attachments != null) {
				for(Map<String, Object> attachment : attachments) {  
					File file=(File)attachment.get("file");
					if(file==null||!file.isFile()||file.length()<=0){
						continue;
					}
					String fileName="";
					Object fileNameObj=attachment.get("fileName");
					if(fileNameObj!=null&&StringUtils.isNotBlank(fileNameObj.toString())){
						fileName=fileNameObj.toString();
					}else{
						fileName=file.getName();
					}
					BodyPart attachmentPart = new MimeBodyPart();  
					DataSource source = new FileDataSource(file);  
					attachmentPart.setDataHandler(new DataHandler(source));  
					//避免中文乱码的处理  
					attachmentPart.setFileName(MimeUtility.encodeWord(fileName));  
					mp.addBodyPart(attachmentPart);  
				}  
			}
			mimeMsg.setContent(mp);
			// 保存邮件
			mimeMsg.saveChanges();
			// 发送邮件
			Transport transport = session.getTransport("smtp");
			transport.connect((String) props.get("mail.smtp.host"), email, password);
			// transport.sendMessage(mimeMsg,
			// mimeMsg.getRecipients(Message.RecipientType.CC));//添加抄送人
			transport.sendMessage(mimeMsg, mimeMsg.getRecipients(Message.RecipientType.TO));// 发送
			transport.close();
			falg = true;
		} catch (Exception e) {
			throw new RuntimeException("发送失败", e);
		}
		return falg;
	}
}
